﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace XQ.Framework.io.image
{
    /// <summary>
    /// 显示原始大小的图片
    /// </summary>
    public class DrawImage
    {
        public DrawImage()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        private string contentType = "image/Jpg";
        private FileStream fs = null;
        private MemoryStream ms = null;
        private FileInfo fi = null;
        public bool autoDispose = true;
        /// <summary>
        /// 画图
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="response"></param>
        public void drawImage(string filePath, HttpResponse response)
        {
            try
            {
                fi = new FileInfo(filePath);
                ms = new MemoryStream();
                if (!fi.Exists)
                {
                    return;
                }
                string ext = fi.Extension.ToLower();
                if (!checkExtentsion(ext))
                {
                    return;
                }
                fs = fi.OpenRead();
                if (fs == null)
                {
                    return;
                }
                byte[] b = new byte[1024];
                int i;
                while ((i = fs.Read(b, 0, b.Length)) > 0)
                {
                    ms.Write(b, 0, b.Length);
                }
                response.ContentType = contentType;
                response.BinaryWrite(ms.ToArray());
                response.Flush();
                response.End();
            }
            catch
            { }
            finally
            {
                if (autoDispose)
                {
                    dispose();
                }
            }
        }

        public void drawImage(byte[] b, HttpResponse response)
        {
            try
            {
                ms = new MemoryStream();
                ms.Write(b, 0, b.Length);
                response.ContentType = contentType;
                response.BinaryWrite(ms.ToArray());
                response.Flush();
                response.End();
            }
            catch
            { }
            finally
            {
                if (autoDispose)
                {
                    dispose();
                }
            }
        }

        private bool checkExtentsion(string str)
        {
            string[] strs = new string[] { ".gif", ".png", ".jpg", ".bmp" };
            for (int i = 0; i < strs.Length; i++)
            {
                if (str == strs[i])
                {
                    contentType = "image/" + str.Substring(1, str.Length - 1);
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 销毁对象
        /// </summary>
        public void dispose()
        {
            if (fs != null)
            {
                fs.Close();
            }
            if (ms != null)
            {
                ms.Close();
            }
        }

    }
}